package cn.clq.ymcc.mq.listener;


import cn.clq.ymcc.constant.PayOrderStateConstants;
import cn.clq.ymcc.domain.PayOrder;
import cn.clq.ymcc.dto.PayNotifyDto;
import cn.clq.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQTransactionListener(
        txProducerGroup = "service-pay-tx-group"
)
@Slf4j
public class PayNotifyRocketMqListener implements RocketMQLocalTransactionListener {
    @Autowired
    private IPayOrderService payOrderService;

    /**
     * @description: 执行本地事务,修改支付单的状态
     * @param: message
    arg
     * @return: org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
     * @author clqlongbao
     * @date: 2022/8/3 23:46
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
        //执行本地事务  修改支付单的状态和时间
        log.info("回调执行本事务，修改支付单的支付状态");
        PayOrder payOrder = (PayOrder) arg;
        PayOrder orderTmp = payOrderService.selectByOrderNo(payOrder.getOrderNo());
        if(orderTmp.getPayStatus().intValue() != PayOrderStateConstants.STATE_WAIT_PAY.intValue()){
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        payOrderService.updateById(payOrder);
        return RocketMQLocalTransactionState.COMMIT;//执行本地事务成功
    }

    /**
     * @description: 检查本地事务，检查本地的支付单状态是否是已支付
     * @param: message
     * @return: org.apache.rocketmq.spring.core.RocketMQLocalTransactionState
     * @author clqlongbao
     * @date: 2022/8/4 1:33
     */
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        log.info("回调检查支付单的支付状态。。");
        try {
            byte[] bodyTmp = (byte[]) message.getPayload();
            String notifyDtoStr = new String(bodyTmp, StandardCharsets.UTF_8);
            PayNotifyDto dto = JSONObject.parseObject(notifyDtoStr, PayNotifyDto.class);
            if(dto != null){
                return RocketMQLocalTransactionState.UNKNOWN;
            }
            PayOrder orderTmp = payOrderService.selectByOrderNo(dto.getOrderNo());
            if(orderTmp == null){
                return RocketMQLocalTransactionState.UNKNOWN;
            }
            //判断支付单的状态是否是已支付(真正的本地事务)
            if(orderTmp.getPayStatus().intValue() == PayOrderStateConstants.STATE_PAY_SUCCESS.intValue()){
                return RocketMQLocalTransactionState.COMMIT;
            }
            return RocketMQLocalTransactionState.ROLLBACK;
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.UNKNOWN;
        }
    }
}
